<template lang="html">
  <div class="search">
    <div class="search-title">
      <input type="text" id="input" placeholder="请输入歌名/歌手……" v-model="searchContent" @input="searchDeBounce">
      <div class="btn">
        <i @click="searchHandler" class="iconfont icon-sousuo"></i>
      </div>
    </div>
    <ul class="list searchlist" ref="resultUl">
     <router-link :key="index" tag="li" :to="{name:'MusicPlay',params:{songId:item.id}}" class="song" v-for="(item,index) in songlist">
       <div class="left">
         <div class="info  single-line ">
             <div>
                 <span>{{ item.name }}</span>
             </div>
             <span class="txt" v-for="(item,index) in item.artists"
             :key="index">{{ item.name }}</span>
         </div>
       </div>
     </router-link>
   </ul>
  </div>
</template>

<script>
import "../assets/font/iconfont.css";
// 防抖
  function  deBounce(fn, delay) {
      let timer = null;
      return function() {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          fn.call(this);
        }, delay);
      };
    }
export default {
  name: "search",
  data() {
    return {
      searchContent: "",
      songlist: []
    };
  },
  methods: {
    // 防抖搜索函数
    searchDeBounce: deBounce(function(){
      this.searchHandler();
    }, 1000),
    // 发送请求函数
    searchHandler() {
      if (document.getElementById("input").value) {
        const searchUrl = this.HOST + `/search?keywords=${this.searchContent}`;
        this.$axios.get(searchUrl).then(res => {
          if (res) {
            let list = res.data.result.songs || null;
            this.songlist = list;
          } else {
            this.$refs.resultUl.innerHTML = `<p>很抱歉！没有找到相关内容</p>`;
          }
        },
        err => console.log('搜索歌曲报错', err.response.data.message));
      }
    }
  }
};
</script>

<style scoped>
.search {
  background-color: rgb(228, 231, 231);
}
.search-title {
  padding: 20px;
  overflow: hidden;
  clear: both;
  position: sticky;
  top: 0;
  background-color: rgb(228, 231, 231);
}

input {
  width: 80%;
  height: 30px;
  line-height: 30px;
  background: #fff;
  border: 1px solid #f1f2f3;
  padding-left: 10px;
  display: inline-block;
}

.btn {
  width: 40px;
  height: 30px;
  float: right;
  margin-left: 10px;
  background-color: rgb(48, 202, 187, 0.5);
  border-radius: 5px;
}

i {
  font-size: 25px;
  padding-left: 7px;
  font-weight: 600;
  color: white;
}

.list {
  top: 72px;
  word-wrap: break-word;
  -webkit-hyphens: auto;
  hyphens: auto;
  word-break: break-all;
  background-color: rgb(228, 231, 231);
  border-bottom: 1px solid #e5e5e5;
  border-top: 1px solid #e5e5e5;
}

.list li.song {
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  min-height: 55px;
  text-align: left;
}

li {
  display: flex;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
  min-height: 50px;
  border-bottom: 1px solid #f2f2f2;
  padding-left: 10px;
}

.list .item.song .left,
.list li.song .left {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list .item .info,
.list li .info {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list li .info > span {
  font-weight: 400;
  display: block;
  font-size: 12px;
  color: #999;
}
</style>